package com.VolunteerPlatform.Service.VolunteerService.Impl;

import com.VolunteerPlatform.Dao.BankInfoDao;
import com.VolunteerPlatform.Dao.VolunteerInfoDao;
import com.VolunteerPlatform.Dao.VolunteerRecordDao;
import com.VolunteerPlatform.Dao.VolunteerSignDao;
import com.VolunteerPlatform.Entity.BankInfo;
import com.VolunteerPlatform.Entity.VolunteerInfo;
import com.VolunteerPlatform.Entity.VolunteerRecord;
import com.VolunteerPlatform.Entity.VolunteerSign;
import com.VolunteerPlatform.Service.UtilityService.ExportDataExcel;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerInfoService;
import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.v2.model.core.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.soap.Name;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by DoveXL on 2018/12/6.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class VolunteerInfoServiceImpl implements VolunteerInfoService{

    @Autowired
    private VolunteerInfoDao volunteerInfoDao;

    @Autowired
    private BankInfoDao bankInfoDao;

    @Autowired
    private VolunteerRecordDao volunteerRecordDao;

    @Autowired
    private VolunteerSignDao volunteerSignDao;

    public List<VolunteerInfo> getVolunteerInfoService(String IDCardNumber, String Name, String Gender, String Unit, String Phonenum,int Deleted, int Page, int PageList)
    {
        String IDCard;
        String PersonName;
        String GenderLike;
        String UnitLike;
        String PhonenumLike;

        if(IDCardNumber!=null)
        {
            IDCard = "%"+IDCardNumber+"%";
        }else
        {
            IDCard=null;
        }
        if(Name!=null)
        {
            PersonName = "%"+Name+"%";
        }else
        {
            PersonName=null;
        }

        if (Gender!=null)
        {
            GenderLike = "%"+Gender+"%";
        }else
        {
            GenderLike=null;
        }

        if(Unit!=null)
        {
            UnitLike = "%"+Unit+"%";
        }else
        {
            UnitLike=null;
        }

        if (Phonenum!=null)
        {
            PhonenumLike = "%"+Phonenum+"%";
        }else
        {
            PhonenumLike=null;
        }
        return volunteerInfoDao.getVolunteerInfo(IDCard,PersonName,GenderLike,UnitLike,PhonenumLike,Deleted,Page,PageList);
    }





    public int updateVolunteerInfoService(String OriginIDCardNumber,String IDCardNumber,String NewName,String Name,String Unit,String Phonenum,int Deleted)
    {
        if(OriginIDCardNumber.equals(IDCardNumber) && Name.equals(NewName))
        {
            return volunteerInfoDao.updateVolunteerInfo(OriginIDCardNumber,IDCardNumber, Name, null, null, Unit, Phonenum, Deleted);
        }else {
            String Gender = "男";
            String Birthday = "1994-05";
            if (IDCardNumber.length() == 15) {
                String GenderNumber = IDCardNumber.substring(14);

                if (Integer.parseInt(GenderNumber) % 2 == 0) {
                    Gender = "女";
                } else {
                    Gender = "男";
                }
                String BirthdayYear = IDCardNumber.substring(6, 8);
                String BirthdayMonth = IDCardNumber.substring(8, 10);
                Birthday = "19" + BirthdayYear + "-" + BirthdayMonth;
            } else if (IDCardNumber.length() == 18) {
                String GenderNumber = IDCardNumber.substring(16, 17);
                if (Integer.parseInt(GenderNumber) % 2 == 0) {
                    Gender = "女";
                } else {
                    Gender = "男";
                }
            }
                String BirthdayYear = IDCardNumber.substring(6, 10);
                String BirthdayMonth = IDCardNumber.substring(10, 12);
                Birthday = BirthdayYear + "-" + BirthdayMonth;
                Map<String, String> map1 = new HashMap<>();
                map1.put("OriginIDCardNumber", OriginIDCardNumber);
                map1.put("IDCardNumber", IDCardNumber);
                map1.put("Name", NewName);
                List<Map<String, String>> UpdateList = new ArrayList<>();
                UpdateList.add(map1);
                int BankInfoupdate = 0;
                int VolunteerRecordUpdate = 0;
                int VolunteerSignUpdate = 0;
                List<BankInfo> bankInfoList = bankInfoDao.getAllBankInfo(OriginIDCardNumber, null, null, null, null, -1, -1, -1);
                List<VolunteerRecord> volunteerRecordList = volunteerRecordDao.getAllVolunteerRecord(OriginIDCardNumber, Name, null, null, null, null, null,-1, -1);
                List<VolunteerSign> volunteerSignList = volunteerSignDao.getAllVolunteerSign(OriginIDCardNumber, Name, null, null, null,-1, null, -1, -1);
                if (bankInfoList.size() != 0) {
                    BankInfoupdate = bankInfoDao.updateBankInfoBatch(UpdateList);
                }
                if (volunteerRecordList.size() != 0) {
                    VolunteerRecordUpdate = volunteerRecordDao.updateVolunteerRecordBatch(UpdateList);
                }
                if (volunteerSignList.size() != 0) {
                    VolunteerSignUpdate = volunteerSignDao.updateVolunteerSignBatch(UpdateList);
                }
                int flag = volunteerInfoDao.updateVolunteerInfo(OriginIDCardNumber, IDCardNumber, Name, Gender, Birthday, Unit, Phonenum, Deleted);
                if (BankInfoupdate == bankInfoList.size() && VolunteerRecordUpdate == volunteerRecordList.size() && volunteerSignList.size() == VolunteerSignUpdate && 1 == flag) {
                    return 1;
                } else {
                    return 0;
                }
            }
    }

    public int deleteVolunteerInfoService(String OriginIDCardNumber,String Name,String Unit,String Phonenum,int Deleted)
    {
        return volunteerInfoDao.updateVolunteerInfo(OriginIDCardNumber,null,null,null,null,null,null,Deleted);
    }



    public int insertVolunteerInfoService(String IDCardNumber,String Name,String Gender,String Birthday,String Unit,String Phonenum,int Deleted)
    {
        int InsertNumber = volunteerInfoDao.insertVolunteerInfo(IDCardNumber, Name, Gender, Birthday, Unit, Phonenum, Deleted);
        if(InsertNumber==1)
        {
            int InsertBankNumber = bankInfoDao.insertBankInfo(IDCardNumber,Name,"无","无");
            return InsertBankNumber;
        }else
        {
            return 0;
        }
    }

    public List<VolunteerInfo> findByIDCardNumberService( String IDCardNumber)
    {
        return volunteerInfoDao.findByIDCardNumber(IDCardNumber);
    }

    public int ExportPersonInfoService(String Name,String IDCardNumber,String Unit,String Gender,String title,String[] rowName,List<String> checkFieldNameList,String filename) throws IOException
    {
        List<BankInfo> BankInfoList = bankInfoDao.getAllBankInfo(IDCardNumber,Name,null,null,Unit,0,-1,-1);
        List<VolunteerInfo> volunteerInfos = volunteerInfoDao.getVolunteerInfo(IDCardNumber,Name,null,Unit,null,0,-1,-1);
        List<Object []> StringList = new ArrayList<>();
        int BankInfoSize = BankInfoList.size();
        int volunteerInfoSize = volunteerInfos.size();
        Map<String,String> map1 = new HashMap<>();
        Map<String,String> map2 = new HashMap<>();
        for(int j=0;j<BankInfoSize;j++)
        {
            map1.put(BankInfoList.get(j).getIDCardNumber(),BankInfoList.get(j).getBankAccount());
            map2.put(BankInfoList.get(j).getIDCardNumber(),BankInfoList.get(j).getBankBranch());
        }

        for(int i=0;i<volunteerInfoSize;i++)
        {
            Map<String,String> VolunteerInfoMap = new HashMap<>();
            VolunteerInfo volunteerInfo =volunteerInfos.get(i);
            VolunteerInfoMap.put("Name",volunteerInfo.getName());
            VolunteerInfoMap.put("IDCardNumber",volunteerInfo.getIDCardNumber());
            VolunteerInfoMap.put("Unit",volunteerInfo.getUnit());
            VolunteerInfoMap.put("Gender",volunteerInfo.getGender());
            VolunteerInfoMap.put("Birthday",volunteerInfo.getBirthday());
            VolunteerInfoMap.put("Phonenum",volunteerInfo.getPhonenum());
            VolunteerInfoMap.put("BankAccount",map1.get(volunteerInfo.getIDCardNumber()));
            VolunteerInfoMap.put("BankBranch",map2.get(volunteerInfo.getIDCardNumber()));
            String[] dataArray = new String[checkFieldNameList.size()];
            List<String> dataList = new ArrayList<>();
            for(int j = 0;j<checkFieldNameList.size();j++)
            {
                dataList.add(VolunteerInfoMap.get(checkFieldNameList.get(j)));
            }
            dataList.toArray(dataArray);
            StringList.add(dataArray);
        }
        ExportDataExcel exportDataExcel = new ExportDataExcel(title,rowName,StringList,filename);
        try {
            exportDataExcel.ExportExcel();
            return 1;
        }catch (Exception e)
        {
            e.printStackTrace();
            throw  new IOException();
        }
    }

    public Map<String,Integer> VolunteerAgeStatistics(String IDCardNumber, String Name, String Gender, String Unit, String Phonenum,int Deleted, int Page, int PageList)
    {
        List<VolunteerInfo> volunteerInfoList = volunteerInfoDao.getVolunteerInfo(IDCardNumber, Name, Gender, Unit, Phonenum, Deleted, Page, PageList);
        int count020=0;
        int count2130=0;
        int count3140=0;
        int count4150=0;
        int count50=0;

        try {
            Calendar now = Calendar.getInstance();
            now.setTime(new Date()); // 当前时间
            for(int i=0;i<volunteerInfoList.size();i++)
            {
                String IDCardNumber1 =volunteerInfoList.get(i).getIDCardNumber();
                String birthday = "";

                if(IDCardNumber1.length()==15)
                {
                        birthday = "19" + IDCardNumber1.substring(6, 8) + "-" + IDCardNumber1.substring(8, 10) + "-" + IDCardNumber1.substring(10, 12);

                }else if (IDCardNumber1.length()==18)
                {
                    birthday =  IDCardNumber1.substring(6, 10) + "-" +  IDCardNumber1.substring(10, 12) + "-" + IDCardNumber1.substring(12, 14);

                }else
                {
                    break;
                }
                int age=0;
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date parse = format.parse(birthday);
                Calendar birth = Calendar.getInstance();
                birth.setTime(parse); // 传入的时间
                if (birth.after(now)) {
                    age = 0;
                } else {
                    age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                    if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                        age += 1;
                    }
                }

                if (age>=0 && age <=20)
                {
                    count020++;
                }else if(age>=21 && age <=30)
                {
                    count2130++;
                }else if(age>=31 && age <=40)
                {
                    count3140++;
                }else if(age>=41 && age<=50)
                {
                    count4150++;
                }else if(age>=51)
                {
                    count50++;
                }else
                {

                }
            }
            Map<String,Integer> AgeStatistics = new HashMap<>();

            AgeStatistics.put("count1",count020);
            AgeStatistics.put("count2",count2130);
            AgeStatistics.put("count3",count3140);
            AgeStatistics.put("count4",count4150);
            AgeStatistics.put("count5",count50);

            return AgeStatistics;
        }catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }


    public String VolunteerTeamNumberStatistics(String IDCardNumber, String Name, String Gender, String Unit, String Phonenum,int Deleted, int Page, int PageList)
    {
        List<VolunteerInfo> volunteerInfoList = volunteerInfoDao.getVolunteerInfo(IDCardNumber, Name, Gender, Unit, Phonenum, Deleted, Page, PageList);
        Map<String,Integer> map = new HashMap<>();
        for (int i =0;i<volunteerInfoList.size();i++)
        {
            VolunteerInfo volunteerInfo = volunteerInfoList.get(i);
            if(map.keySet().contains(volunteerInfo.getUnit()))
            {
                map.put(volunteerInfo.getUnit(),map.get(volunteerInfo.getUnit())+1);
            }else
            {
                map.put(volunteerInfo.getUnit(),1);
            }
        }
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (String e:map.keySet()) {
            Map<String,Object> map1 = new HashMap<>();
            map1.put("name",e);
            map1.put("number",map.get(e));
            mapList.add(map1);
        }

        return JSON.toJSONString(mapList);
    }



}
